package com.ytt.core.service.entity.role;

import com.ytt.core.service.entity.AbstractBaseEntity;
import com.ytt.core.service.entity.authority.Authority;
import com.ytt.core.service.entity.user.User;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * Role
 *
 * @author chenwen
 * @date 2019/9/17 10:49
 */
@Entity
@Table(name = "sys_role")
@Getter
@Setter
public class Role extends AbstractBaseEntity {
    /**
     * 名称, 要求唯一性
     */
    @Column(name = "name", length = 50, nullable = false, unique = true)
    private String name;

    /**
     * 备注，说明该Role应当分配给什么样的人使用
     */
    @Column(name = "remark", length = 500)
    private String remark;

    /**
     * 角色所包含的用户
     */
    @ManyToMany(mappedBy = "roles")
    @OrderBy("name asc")
    private Set<User> users = new LinkedHashSet<>();

    /**
     * 角色关联的权限
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "sys_role_authority", joinColumns = @JoinColumn(name = "role_id"), inverseJoinColumns = @JoinColumn(name = "authority_id"))
    @OrderBy("name asc")
    private Set<Authority> authorities = new LinkedHashSet<>();


    @Override
    public boolean equals(Object o) {
        return super.equals(o);
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }
}
